package com.wl156.adapters;

import org.ksoap2.serialization.SoapObject;

import com.wl156.R;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

public class SpecialLineAdapter extends CheckableAdapter implements
		OnClickListener {

	public SpecialLineAdapter(Context context, SoapObject data) {
		this(context, data, DEFAULT);
	}

	public SpecialLineAdapter(Context context, SoapObject data, int type) {
		mInflater = LayoutInflater.from(context);
		mData = data;
		mContext = context;
		mType = type;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.item_special_line, null);
		}

		TextView from_to = (TextView) convertView.findViewById(R.id.from_to);
		TextView companyName = (TextView) convertView
				.findViewById(R.id.companyName);

		TextView address = (TextView) convertView.findViewById(R.id.address);
		TextView contact = (TextView) convertView.findViewById(R.id.contact);

		TextView phoneDial = (TextView) convertView.findViewById(R.id.dial);
		phoneDial.setOnClickListener(this);

		// 06-08 12:16:18.828: D/(27659): result:
		// anyType{ArrayOfString=anyType{string=4506; string=广州市忠程物流有限公司;
		// string=广东广州; string=北京北京; string=肖荣生; string=13189053711;
		// string=null; string=anyType{}; string=anyType{}; string=13189053711;
		// string=林短; string=二星级; string=0; };
		// ArrayOfString=anyType{string=4504; string=广州市忠程物流有限公司; string=广东广州;
		// string=辽宁大连; string=肖荣生; string=13189053711; string=null;
		// string=anyType{}; string=anyType{}; string=13189053711; string=林短;
		// string=二星级; string=0; }; ArrayOfString=anyType{string=4505;
		// string=广州市忠程物流有限公司; string=广东广州; string=河南新乡; string=肖荣生;
		// string=13189053711; string=null; string=anyType{}; string=anyType{};
		// string=13189053711; string=林短; string=二星级; string=0; };
		// ArrayOfString=anyType{string=4502; string=广州市中实货运代理有限公司; string=广东广州;
		// string=北京北京; string=刘生; string=18675977238;
		// string=广州市番禺区东环街番禺大道北941号华圣物流园8栋13、15、16号; string=anyType{};
		// string=anyType{}; string=18675977238; string=林短; string=二星级;
		// string=0; }; ArrayOfString=anyType{string=4503; string=广州市中实货运代理有限公司;
		// string=广东广州; string=河南郑州; string=刘生; string=18675977238;
		// string=广州市番禺区东环街番禺大道北941号华圣物流园8栋13、15、16号; string=anyType{};
		// string=anyType{}; string=18675977238; string=林短; string=二星级;
		// string=0; }; ArrayOfString=anyType{string=4500; string=广州市忠程物流有限公司;
		// string=广东广州; string=福建泉州; string=肖荣生; string=13189053711;
		// string=null; string=anyType{}; string=anyType{}; string=13189053711;
		// string=林短; string=二星级; string=0; };
		// ArrayOfString=anyType{string=4501; string=广州市康亚物流有限公司; string=广东广州;
		// string=江苏南京; string=廖鹏斐 副总; string=13711259856;
		// string=广州市白云区太和镇谢家庄村第八经济合作社自编号锦邦货运市场A3区3317号; string=anyType{};
		// string=anyType{}; string=13711259856; string=林短; string=二星级;
		// string=0; }; ArrayOfString=anyType{string=4498; string=广州市忠程物流有限公司;
		// string=广东广州; string=安徽合肥; string=肖荣生; string=13189053711;
		// string=null; string=anyType{}; string=anyType{}; string=13189053711;
		// string=林短; string=二星级; string=0; };
		// ArrayOfString=anyType{string=4499; string=广州市忠程物流有限公司; string=广东广州;
		// string=甘肃兰州; string=肖荣生; string=13189053711; string=null;
		// string=anyType{}; string=anyType{}; string=13189053711; string=林短;
		// string=二星级; string=0; }; }, type: SoapObject

		int phoneIndex = 0;
		switch (mType) {
		case DEFAULT:
			from_to.setText(String.format("%s=>%s",
					getTextViewStringFromProperty(position, 2),
					getTextViewStringFromProperty(position, 3)));
			setTextViewStringFromProperty(position, companyName, 1);
			setTextViewStringFromProperty(position, address, 6);
			setTextViewStringFromProperty(position, contact, 4);
			phoneIndex = 5;
			break;
		case COLLECT:
			from_to.setText(String.format("%s=>%s",
					getTextViewStringFromProperty(position, 3),
					getTextViewStringFromProperty(position, 4)));
			setTextViewStringFromProperty(position, companyName, 2);
			setTextViewStringFromProperty(position, address, 7);
			setTextViewStringFromProperty(position, contact, 5);
			phoneIndex = 6;
			break;
		}
		setPhoneView(position, phoneDial, phoneIndex);

		super.getView(position, convertView, parent);

		if (position % 2 != 0) {
			convertView.setBackgroundColor(Color.parseColor("#F0FAFE"));
		} else {
			convertView.setBackgroundColor(Color.parseColor("#FFFFFF"));
		}

		return convertView;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.dial:
			Intent i = new Intent(Intent.ACTION_CALL);
			i.setData(Uri.parse("tel:" + v.getTag().toString()));
			mContext.startActivity(i);
			break;
		}
	}
}
